Multi-lingual Application

Add Dropdown List To Choose Language Of Application

Description
This customization shows how to add a dropdown list to change language(UI culture) of your application.
Variables
Applies to
Page class
Code
 
      /// 
      /// This method is called when a page is initialized.
      /// 
      public void MyInit(object sender, System.EventArgs e)
      {
        this.LanguageList.SelectedIndexChanged +=new EventHandler(LanguageList_SelectedIndexChanged);
      }
    
Applies to
Page class
Code
 
/// 
/// The MyPage_Load function handles load event for the page..
/// 
/// The object that raised the load event.
/// The object that contains the event data of the load event.
private void MyPage_Load(System.Object sender, System.EventArgs e)
{
    if (!(this.Page.IsPostBack))
    {

        // Get the resource directory of the application. It is "bin" for .NET Framework 1.1 applications
        // and "App_GlobalResources" for .NET Framework 2.0 applications
        string  resourceDir = "";
        string runtimeVersion = System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion();
        if (runtimeVersion.IndexOf("v2") == -1)
        {
            resourceDir = "bin";
        }
        else
        {
            resourceDir = "App_GlobalResources";
        }
        string resourcePath  = this.Request.PhysicalApplicationPath + resourceDir + "\\";
        if (System.IO.Directory.Exists(resourcePath))
        {

            // Find all the resource files of the type AppName.culture.resx
            string[] resourceFiles  = System.IO.Directory.GetFiles(resourcePath, "*.*.resx");

            foreach (string resourceFile in resourceFiles) 
            {

                // collect the resource objects and count the occurrences of the names
                System.IO.FileInfo	finfo = new  System.IO.FileInfo(resourceFile);

                // Get the file name
                string fileName	= finfo.Name;

                // Split the filename by the "."
                string splitBy = ".";
                string[] namePieces	= fileName.Split(splitBy.ToCharArray());

                int length = namePieces.Length;
                int	index = length - 2;

                // Get the culture name portion of the filename like "en-US"
                string  cultureName	= namePieces[index];

                // Create a CultureInfo object to represent the culture
                System.Globalization.CultureInfo  culutreInfo   = new System.Globalization.CultureInfo(cultureName);

                // Populate the cultures list
                ListItem li  = new ListItem(culutreInfo.DisplayName, culutreInfo.Name);
                if (!(this.LanguageList.Items.Contains(li)))
                {
                    this.LanguageList.Items.Add(li);
                    if (System.Threading.Thread.CurrentThread.CurrentUICulture.Name == culutreInfo.Name) 
                    {
                        // Select the current culture of the application
                        li.Selected = true;
                    }
                }
            			
            }
        }
    }
}
    
     
Applies to
Page class
Code
 
	/// 
	/// The LanguageList_SelectedIndexChanged function handles SelectedIndexChanged event for the custom button.
	/// 
	/// The object that raised the SelectedIndexChanged event.
	/// The object that contains the event data of the SelectedIndexChanged event.
	private void LanguageList_SelectedIndexChanged(object sender, System.EventArgs e) 
	{ 
		// Base Page sets the culture for the current thread by reading the two session variables 
		// "AppCulture" and "AppCultureUI"
		Session["AppCulture"] = this.LanguageList.SelectedItem.Value;
		Session["AppCultureUI"] = this.LanguageList.SelectedItem.Value;
		
		//The culture is applied to the page in Page_Init of BasePage. Therefore, redirecting back to same page.
		this.Page.Response.Redirect(Request.Url.PathAndQuery);
    						
	}
     
Applies to
CSharpPageConstructor class
Code
 
    // The following line will be inserted inside the
    // constructor for page class.
    this.Init += new System.EventHandler(this.MyInit);
    this.Load += new System.EventHandler(this.MyPage_Load);
     

Terms of Service Privacy Statement